package com.squareshoot.picCheckin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VenuePhotosAdapter extends ArrayAdapter<String> {
	int layout;
	Context context;
	ArrayList<String> venuePhotos;

	PhotosDownloader photosDl;

	static class ViewHolder{
		ImageView photo;
		TextView shout;
		TextView date;
		TextView user;
		TextView title;
	}

	public VenuePhotosAdapter(Context context, ArrayList<String> objects) {
		super(context, R.layout.venuephotosadapter, objects);

		this.layout = R.layout.venuephotosadapter;
		this.context = context;
		this.venuePhotos = objects;

		photosDl = new PhotosDownloader(false,true,getContext().getResources().getDrawable(R.drawable.loadingphoto));
	}

	public View getView(int position,View convertView,ViewGroup parent){
		ViewHolder holder;
		String photo = venuePhotos.get(position);
		JSONObject jPhoto;

		if(convertView==null){
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView=inflater.inflate(layout, parent, false);

			holder = new ViewHolder();
			holder.photo = (ImageView) convertView.findViewById(R.id.venuePhoto);
			holder.shout = (TextView) convertView.findViewById(R.id.venuePhotosShout);
			holder.date = (TextView) convertView.findViewById(R.id.venuePhotosDate);
			holder.user = (TextView) convertView.findViewById(R.id.venuePhotosUser);
			holder.title = (TextView) convertView.findViewById(R.id.venuePhotosTitle); 	
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}

		try {
			jPhoto = new JSONObject(photo);

			if(position==0){
				String group = context.getString(R.string.venueMostViewed);
				holder.title.setVisibility(View.VISIBLE);
				holder.title.setText(group);
			}else if (position==1){
				String group = context.getString(R.string.venuePhotos);
				holder.title.setVisibility(View.VISIBLE);
				holder.title.setText(group);
			}else{
				holder.title.setVisibility(View.GONE);
			}
			
			photosDl.getPhoto(Common.getBaseImgsUrl()+"images/thumbs/"+jPhoto.getString("filename"), holder.photo);

			if(jPhoto.has("shout")){
				holder.shout.setText(jPhoto.getString("shout"));
			}

			Date date;
			try {
				SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
				date=parser.parse(jPhoto.getString("date"));
				SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM kk:mm Z");
				holder.date.setText(formatter.format(date));
			} catch (ParseException e) {
				Log.d(Common.TAG,e.getMessage());
			}
			
//			long millis = Long.parseLong(jCheckin.getString("millis"))*1000;
//			Date date = new Date(millis);
			
			if(jPhoto.getString("username").length()>0)
				holder.user.setText("by "+jPhoto.getString("username"));
		} catch (JSONException e1) {
			if(Common.HIGHDEBUG)
				Log.d(Common.TAG,"error JSON in PhotosAdapter : "+e1.getMessage());
		}

		return convertView;
	}
}

